﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace PL_TileEditor
{
    public partial class ItemMaker : Form
    {
        private class ModStat
        {
            public string Stat;
            public int Value;
            public override string ToString()
            {
                return Stat + " { " + Value.ToString() + " } ";
            }
        };

        public ItemMaker()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
                pictureBox1.Tag = openFileDialog1.FileName;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Enabled = true;
            Item tempItem = new Item();
            tempItem.Name = "NoName";
            listBox1.Items.Add(tempItem);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                groupBox1.Enabled = true;
                button2.Enabled = true;
                textBox2.Text = ((Item)(listBox1.SelectedItem)).Name;
                textBox4.Text = ((Item)(listBox1.SelectedItem)).Description;
                numericUpDown2.Value = ((Item)(listBox1.SelectedItem)).BuyPrice;
                numericUpDown3.Value = ((Item)(listBox1.SelectedItem)).SellPrice;
                if (((Item)(listBox1.SelectedItem)).IconPath != "ERROR")
                {
                    pictureBox1.Image = Image.FromFile(((Item)(listBox1.SelectedItem)).IconPath);
                }
                else
                {
                    pictureBox1.Image = PL_TileEditor.Properties.Resources.Unknown;
                }
                listBox2.Items.Clear();
                listBox3.Items.Clear();
                for (int i = 0; i < ((Item)(listBox1.SelectedItem)).ModStats.Count; i++)
                {
                    ModStat tempMod = new ModStat();
                    tempMod.Stat = ((Item)(listBox1.SelectedItem)).ModStats.Keys.ToArray()[i];
                    tempMod.Value = ((Item)(listBox1.SelectedItem)).ModStats.Values.ToArray()[i];
                    listBox2.Items.Add(tempMod);
                }
                for (int i = 0; i < ((Item)(listBox1.SelectedItem)).Buffs.Count; i++)
                {
                    ModStat tempMod = new ModStat();
                    tempMod.Stat = ((Item)(listBox1.SelectedItem)).Buffs.Keys.ToArray()[i];
                    tempMod.Value = ((Item)(listBox1.SelectedItem)).Buffs.Values.ToArray()[i];
                    listBox3.Items.Add(tempMod);
                }
            }
            else
            {
                groupBox1.Enabled = false;
                button2.Enabled = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Remove(listBox1.SelectedItem);
            if (listBox1.Items.Count == 0)
            {
                listBox1.Enabled = false;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ModStat tempMod = new ModStat();
            tempMod.Stat = textBox1.Text;
            tempMod.Value = (int)(numericUpDown1.Value);
            listBox2.Items.Add(tempMod);
            listBox2.Enabled = true;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != null)
            {
                button3.Enabled = true;
            }
            else
            {
                button3.Enabled = false;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedItem != null)
            {
                listBox2.Items.Remove(listBox2.SelectedItem);
                if (listBox2.Items.Count == 0)
                    button4.Enabled = false;
                ((Item)(listBox1.SelectedItem)).ModStats.Remove(((ModStat)(listBox2.SelectedItem)).Stat);
            }
        }

        private void APPLY_Click(object sender, EventArgs e)
        {
            Item tempItem = ((Item)(listBox1.SelectedItem));
            tempItem.Name = textBox2.Text;
            tempItem.BuyPrice = (int)numericUpDown2.Value;
            tempItem.SellPrice = (int)numericUpDown3.Value;
            tempItem.IconPath = pictureBox1.Tag.ToString();
            tempItem.Description = textBox4.Text;
            for (int i = 0; i < listBox2.Items.Count; i++)
            {
                tempItem.ModStats.Add(((ModStat)(listBox2.Items[i])).Stat, ((ModStat)(listBox2.Items[i])).Value);
            }
            for (int i = 0; i < listBox3.Items.Count; i++)
            {
                tempItem.Buffs.Add(((ModStat)(listBox3.Items[i])).Stat, ((ModStat)(listBox3.Items[i])).Value);
            }
            listBox1.Items[listBox1.SelectedIndex] = tempItem;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (textBox3.Text != null)
            {  
                button5.Enabled = true;
            }
            else
            {
                button5.Enabled = false;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ModStat tempMod = new ModStat();
            tempMod.Stat = textBox3.Text;
            tempMod.Value = (int)(numericUpDown4.Value);
            listBox3.Items.Add(tempMod);
            listBox3.Enabled = true;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (listBox3.SelectedItem != null)
            {
                listBox3.Items.Remove(listBox3.SelectedItem);
                if (listBox3.Items.Count == 0)
                    button6.Enabled = false;
                ((Item)(listBox1.SelectedItem)).ModStats.Remove(((ModStat)(listBox2.SelectedItem)).Stat);
            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox2.SelectedItem != null)
            {
                button4.Enabled = true;
            }
            else
            {
                button4.Enabled = false;
            }
        }

        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox3.SelectedItem != null)
            {
                button6.Enabled = true;
            }
            else
            {
                button6.Enabled = false;
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {

                FileStream fsFile = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fsFile);
                foreach (Item i in listBox1.Items)
                {
                    bw.Write("Item");
                    bw.Write(i.Name);
                    bw.Write(i.Description);
                    bw.Write(i.IconPath);
                    bw.Write(i.BuyPrice);
                    bw.Write(i.SellPrice);
                    bw.Write(i.ModStats.Count);
                    for (int n=0; n<i.ModStats.Count; n++) 
                    {
                        bw.Write(i.ModStats.Keys.ToArray()[n]);
                        bw.Write(i.ModStats.Values.ToArray()[n]);
                    }
                    bw.Write(i.Buffs.Count);
                    for (int n = 0; n < i.Buffs.Count; n++)
                    {
                        bw.Write(i.Buffs.Keys.ToArray()[n]);
                        bw.Write(i.Buffs.Values.ToArray()[n]);
                    }
                }
                fsFile.Close();
            }
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                FileStream fsFile = new FileStream(openFileDialog2.FileName, FileMode.Open);
                BinaryReader br = new BinaryReader(fsFile);
                br.ReadString();


            }
        }


    }
}
